/*
 * 文 件 名:  XmlParse.java
 * 版    权:  Chrhc Technologies Co., Ltd. Copyright 2015-2099,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人: 姓名 xxx
 * 修改时间:  2016年4月19日
 */
package com.struts;

import java.io.File;
import java.util.*;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * <一句话功能简述> <功能详细描述>
 * 
 * @author 姓名 xxx
 * @version [1.0, 2016年4月19日]
 */
public class XmlParse
{
    
    public static Map<String, ActionMapping> parse(String file,List<String> scanList)
    {
        Map actionMap = new HashMap();
        SAXReader reader = new SAXReader();
        try
        {
            Document document = reader.read(new File(file));
            Element node = document.getRootElement();
            
            Map formMap = new HashMap();
            listNodes(node, actionMap, formMap, null,scanList);
           // Element element = node.element("form-beans");
        }
        catch (DocumentException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return actionMap;
    }
    
    /**
     * 遍历当前节点元素下面的所有(元素的)子节点
     * 
     * @param node
     */
    public static void listNodes(Element node, Map<String, ActionMapping> actionMap, Map formMap,
        ActionMapping parentBean,List<String> scanList)
    {
        // System.out.println("当前节点的名称：：" + node.getName());
        String nodeName = node.getName();
        Map tem = new HashMap();
        // 获取当前节点的所有属性节点
        List<Attribute> list = node.attributes();
        // 遍历属性节点
        for (Attribute attr : list)
        {
            // System.out.println(attr.getText() + "-----" + attr.getName() + "---" + attr.getValue());
            /*
             * if (nodeName.equals("form-bean")) {
             * 
             * }
             */
            tem.put(attr.getName(), attr.getValue());
        }
        
        if (nodeName.equals("form-bean"))
        {
            formMap.put(tem.get("name"), tem.get("type"));// 按照name为key，放置type
        }
        
        ActionMapping bean = null;
        if (nodeName.equals("action"))
        {
            bean = new ActionMapping();
            bean.setName((String)tem.get("name"));
            bean.setPath((String)tem.get("path"));
            bean.setType((String)tem.get("type"));
            String formBeanType = (String)formMap.get(bean.getName());
            bean.setFormBeanType(formBeanType);
            
            actionMap.put(bean.getPath(), bean);// 按照pat放置xml bean
        }
        if (nodeName.equals("forward"))
        {
            Map<String, ActionForward> forwards = null;
            
            if (parentBean != null)
            {
                forwards = parentBean.getActionForwards();
                // forwards.put((String)tem.get("name"), (String)tem.get("path"));
                ActionForward forward = new ActionForward();
                forward.setName((String)tem.get("name"));
                forward.setPath((String)tem.get("path"));
                forwards.put(forward.getName(), forward);
            }
        }
        /*
         * if (!(node.getTextTrim().equals(""))) { System.out.println("文本内容：：：：" + node.getText()); }
         */

        if(nodeName.equals("base-package")){
            if(scanList ==null){
                scanList = new ArrayList<String>();
            }
            scanList.add((String)tem.get("path"));
        }
        // 当前节点下面子节点迭代器
        Iterator<Element> it = node.elementIterator();
        // 遍历
        while (it.hasNext())
        {
            // 获取某个子节点对象
            Element e = it.next();
            // 对子节点进行遍历
            listNodes(e, actionMap, formMap, bean,scanList);
        }
    }
    
    /**
     * <一句话功能简述> <功能详细描述>
     * 
     * @param args
     */
    public static void main(String[] args)
    {
        // TODO Auto-generated method stubre
        String relativelyPath = System.getProperty("user.dir");
        System.out.println(relativelyPath);
        String file = relativelyPath + "/WebContent/WEB-INF/struts-config.xml";
        Map xmlmap = XmlParse.parse(file,null);
        // System.out.println("xmlmap:" + xmlmap);
        Iterator it = xmlmap.entrySet().iterator();
        Map.Entry<String, ActionMapping> entry = null;
        while (it.hasNext())
        {
            entry = (Map.Entry<String, ActionMapping>)it.next();
            ActionMapping b = entry.getValue();
            System.out.println("getActionName:" + b.getName());
            System.out.println("getActionType:" + b.getType());
            System.out.println("getFormBeanType:" + b.getFormBeanType());
            System.out.println("getPath:" + b.getPath());
            System.out.println("getForwards:" + b.getActionForwards());
        }
    }
    
}
